package edu.guigu.appquickstart.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.Map;
import java.util.Set;

/**
 * SharedPreferences 工具类
 * 封装了SharedPreferences的基本操作
 *
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/12 9:48
 */
public class SPUtils {
    private static final String DEFAULT_SP_NAME = "sp_config";
    private static volatile SPUtils sInstance;
    private SharedPreferences mSharedPreferences;

    /**
     * 私有构造方法
     */
    private SPUtils(Context context) {
        this(context, DEFAULT_SP_NAME);
    }

    private SPUtils(Context context, String spName) {
        if (context == null) {
            throw new IllegalArgumentException("Context can not be null.");
        }
        if (TextUtils.isEmpty(spName)) {
            spName = DEFAULT_SP_NAME;
        }
        mSharedPreferences = context.getApplicationContext()
                .getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    /**
     * 获取默认SPUtils实例(默认spName为sp_config)
     */
    public static SPUtils getInstance(Context context) {
        if (sInstance == null) {
            synchronized (SPUtils.class) {
                if (sInstance == null) {
                    sInstance = new SPUtils(context);
                }
            }
        }
        return sInstance;
    }

    /**
     * 获取自定义SPUtils实例
     *
     * @param spName sp文件名
     */
    public static SPUtils getInstance(Context context, String spName) {
        return new SPUtils(context, spName);
    }

    /**
     * 保存数据
     */
    public void put(String key, Object value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof Set) {
            editor.putStringSet(key, (Set<String>) value);
        } else {
            editor.putString(key, value.toString());
        }
        editor.apply();
    }

    /**
     * 获取String类型数据
     */
    public String getString(String key) {
        return getString(key, "");
    }

    public String getString(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    /**
     * 获取int类型数据
     */
    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    /**
     * 获取boolean类型数据
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    /**
     * 获取float类型数据
     */
    public float getFloat(String key) {
        return getFloat(key, 0.0f);
    }

    public float getFloat(String key, float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    /**
     * 获取long类型数据
     */
    public long getLong(String key) {
        return getLong(key, 0L);
    }

    public long getLong(String key, long defaultValue) {
        return mSharedPreferences.getLong(key, defaultValue);
    }

    /**
     * 获取Set<String>类型数据
     */
    public Set<String> getStringSet(String key) {
        return getStringSet(key, null);
    }

    public Set<String> getStringSet(String key, Set<String> defaultValue) {
        return mSharedPreferences.getStringSet(key, defaultValue);
    }

    /**
     * 查询某个key是否已经存在
     */
    public boolean contains(String key) {
        return mSharedPreferences.contains(key);
    }

    /**
     * 获取所有的键值对
     */
    public Map<String, ?> getAll() {
        return mSharedPreferences.getAll();
    }

    /**
     * 移除某个key值已经对应的值
     */
    public void remove(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        editor.apply();
    }

    /**
     * 清除所有数据
     */
    public void clear() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
}
